//
//  TabBarViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/25.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "TabBarViewController.h"

#import "AppDelegate.h"

#import "BaseNavigationViewController.h"

#import "ChannelViewController.h"
#import "FoundViewController.h"
#import "OriginalPublishVC.h"
//#import "OriginalPublishViewController.h"
#import "ThreeDPublishViewController.h"
#import "WishListViewController.h"
#import "AddressManageViewController.h"

@interface TabBarViewController (){
    NSInteger index;//上一次点击的索引
}

@property (nonatomic, strong) UIView *publishView;

@end

@implementation TabBarViewController

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    [self removeSubBtn];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self removeSubBtn];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBar.backgroundColor = [UIColor clearColor];
    index = 100;
    [self createSubController];
    [self removeSubBtn];
    [self createTabBarButton];
    
//    [kNotificationCenter addObserver:self selector:@selector(turnToWishList) name:@"TurnToWishList" object:nil];
//    [kNotificationCenter addObserver:self selector:@selector(turnToAddressList) name:@"TurnToAddressList" object:nil];
//    [kNotificationCenter addObserver:self selector:@selector(back) name:@"ExitUnity" object:nil];
}

- (void)dealloc {
//    [kNotificationCenter removeObserver:self name:@"TurnToWishList" object:nil];
//    [kNotificationCenter removeObserver:self name:@"TurnToAddressList" object:nil];
//    [kNotificationCenter removeObserver:self name:@"ExitUnity" object:nil];
}

- (void)turnToWishList {
//    AppDelegate *delegate = (AppDelegate *)kAppDelegate;
//    [delegate hideUnityWindow];
//    UnityPause(true);
    
    UIButton *btn = (UIButton *)[self.tabBar viewWithTag:104];
    UIButton *lastBtn = (UIButton *)[self.tabBar viewWithTag:index];
    lastBtn.selected = NO;
    btn.selected = YES;
    
    index = btn.tag;
    self.selectedIndex = btn.tag - 100;
    self.tabBar.hidden = NO;
    
    WishListViewController *vc = [[WishListViewController alloc] init];
    [self.childViewControllers[index - 100] pushViewController:vc animated:YES];
}

- (void)turnToAddressList {
//    AppDelegate *delegate = (AppDelegate *)kAppDelegate;
//    [delegate hideUnityWindow];
//    UnityPause(true);
    
    UIButton *btn = (UIButton *)[self.tabBar viewWithTag:104];
    UIButton *lastBtn = (UIButton *)[self.tabBar viewWithTag:index];
    lastBtn.selected = NO;
    btn.selected = YES;
    
    index = btn.tag;
    self.selectedIndex = btn.tag - 100;
    self.tabBar.hidden = NO;
    
    AddressManageViewController *vc = [[AddressManageViewController alloc] init];
    vc.isFromUnity = YES;
    [self.childViewControllers[index - 100] pushViewController:vc animated:YES];
}

#pragma mark - tabbar
- (void)removeSubBtn {
    for (UIView *view in self.tabBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [view removeFromSuperview];
        }
    }
}

- (void)createSubController {
    NSMutableArray *aryControllers = [NSMutableArray array];
    
    BaseNavigationViewController *channelNav = [[BaseNavigationViewController alloc] initWithRootViewController:[[ChannelViewController alloc] init]];
    [aryControllers addObject:channelNav];
    
    BaseNavigationViewController *foundNav = [[BaseNavigationViewController alloc] initWithRootViewController:[[FoundViewController alloc] init]];
    [aryControllers addObject:foundNav];
    
    BaseNavigationViewController *navNav = [[BaseNavigationViewController alloc] initWithRootViewController:[[FoundViewController alloc] init]];
    [aryControllers addObject:navNav];
    
    self.viewControllers = aryControllers;
}

- (void)createTabBarButton {
    NSArray *nameArray = @[@"频道",@"",@"发现"];
    NSArray *imageArray = @[@"频道-未选中",@"发布",@"发现-未选中"];
    NSArray *selectedImageArray = @[@"选中频道",@"发布",@"发现-选中"];
    
    CGFloat itemWidth = SCREEN_WIDTH / nameArray.count;
    for (int i = 0; i < nameArray.count; i++) {
        UIButton *tabBarButton = [[UIButton alloc] initWithFrame:CGRectMake(i * itemWidth, 0, itemWidth, 49)];
        [tabBarButton setTitle:nameArray[i] forState:UIControlStateNormal];
        [tabBarButton setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        [tabBarButton setImage:[UIImage imageNamed:selectedImageArray[i]] forState:UIControlStateSelected];
        tabBarButton.tag = 100 + i;
        tabBarButton.titleLabel.font = [UIFont systemFontOfSize:10];
        tabBarButton.backgroundColor = [UIColor whiteColor];
        [tabBarButton setTitleColor:REXADECIMALCOLOR(@"#ff2469") forState:UIControlStateSelected];
        [tabBarButton setTitleColor:REXADECIMALCOLOR(@"#787878") forState:UIControlStateNormal];
        
        CGSize imgViewSize,titleSize,btnSize;
        UIEdgeInsets imageViewEdge,titleEdge;
        CGFloat heightSpace = 10.0f;
        
        //设置按钮内边距
        imgViewSize = tabBarButton.imageView.bounds.size;
        titleSize = tabBarButton.titleLabel.bounds.size;
        btnSize = tabBarButton.bounds.size;
        imageViewEdge = UIEdgeInsetsMake(5,0.0, btnSize.height -imgViewSize.height - heightSpace, - titleSize.width);
        if (i == 1) {
            imageViewEdge = UIEdgeInsetsMake(-1, 0, 1, 0);
        }
        titleEdge = UIEdgeInsetsMake(30, - imgViewSize.width, 0.0, 0.0);
        [tabBarButton setImageEdgeInsets:imageViewEdge];
        [tabBarButton setTitleEdgeInsets:titleEdge];
        
        if (i == 0) {
            tabBarButton.selected = YES;
        }
        [tabBarButton addTarget:self action:@selector(tabBarAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:tabBarButton];
    }
}

- (void)tabBarAction:(UIButton *)button {
    if (button.tag == 101) {
        if (!_publishView) {
            _publishView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            _publishView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
            
            UIView *allBg = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - ScreenX375(160), SCREEN_WIDTH, ScreenX375(160))];
            allBg.backgroundColor = [UIColor whiteColor];
            [_publishView addSubview:allBg];
            
            NSArray *imageName = @[@"原创发布",@"建模发布"];
            NSArray *titleName = @[@"原创发布",@"建模发布"];
            for (int i = 0; i < imageName.count; i++) {
                UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(78) + i * ScreenX375(164), ScreenX375(25), ScreenX375(65), ScreenX375(100))];
                [button setTitle:titleName[i] forState:UIControlStateNormal];
                [button setTitleColor:FONTCOLOR_BLACK forState:UIControlStateNormal];
                [button setImage:ImageWithName(imageName[i]) forState:UIControlStateNormal];
                button.titleLabel.font = Kfont(14);
                button.tag = 200 + i;
                [button addTarget:self action:@selector(publishAction:) forControlEvents:UIControlEventTouchUpInside];
                
                CGSize imgViewSize,titleSize,btnSize;
                UIEdgeInsets imageViewEdge,titleEdge;
                CGFloat heightSpace = 10.0f;
                
                //设置按钮内边距
                imgViewSize = button.imageView.bounds.size;
                titleSize = button.titleLabel.bounds.size;
                btnSize = button.bounds.size;
                imageViewEdge = UIEdgeInsetsMake(0,0.0, btnSize.height -imgViewSize.height - heightSpace, - titleSize.width);
                titleEdge = UIEdgeInsetsMake(30, - imgViewSize.width, -30, 0.0);
                [button setImageEdgeInsets:imageViewEdge];
                [button setTitleEdgeInsets:titleEdge];
                [allBg addSubview:button];
            }
            
            UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(30)) / 2, ScreenX375(120), ScreenX375(30), ScreenX375(30))];
            [closeBtn setImage:ImageWithName(@"关闭") forState:UIControlStateNormal];
            [closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
            [allBg addSubview:closeBtn];
        }
        [kKeyWindow addSubview:_publishView];
    }else {
        UIButton *lastBtn = (UIButton *)[self.tabBar viewWithTag:index];
        lastBtn.selected = NO;
        button.selected = YES;
        
        index = button.tag;
        self.selectedIndex = button.tag - 100;
        self.tabBar.hidden = NO;
    }
}

- (void)setMySelectIndex:(NSInteger)mySelectIndex {
    UIButton *button = (UIButton *)[self.tabBar viewWithTag:mySelectIndex + 100];
    UIButton *lastBtn = (UIButton *)[self.tabBar viewWithTag:index];
    lastBtn.selected = NO;
    button.selected = YES;
    
    index = button.tag;
    self.selectedIndex = button.tag - 100;
    self.tabBar.hidden = NO;
}

- (void)close {
    [_publishView removeFromSuperview];
}

- (void)publishAction:(UIButton *)button {
    [self close];
    if (button.tag == 200) {
        OriginalPublishVC *vc = [[OriginalPublishVC alloc] init];
        [self.childViewControllers[index - 100] pushViewController:vc animated:YES];
    }else {
//        AppDelegate *delegate = (AppDelegate *)kAppDelegate;
//        [delegate showUnityWindow];
//        UnityPause(false);
        
//        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 44, 44)];
//        [button setImage:ImageWithName(@"返回") forState:UIControlStateNormal];
//        [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
//        [kKeyWindow addSubview:button];
//        
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [kKeyWindow bringSubviewToFront:button];
//        });
        
        ThreeDPublishViewController * vc = [[ThreeDPublishViewController alloc] init];
        [self.childViewControllers[index - 100] pushViewController:vc animated:YES];
        
    }
}

//- (void)back {
//    AppDelegate *delegate = (AppDelegate *)kAppDelegate;
//    [delegate hideUnityWindow];
//    UnityPause(true);
//    [button removeFromSuperview];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
